package cn.edu.zjicm.wordsnet_d.activity.base;

import android.graphics.Color;
import android.os.Bundle;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.ViewFlipper;
import cn.edu.zjicm.wordsnet_d.R;
import cn.edu.zjicm.wordsnet_d.util.NightModeUtil;
import cn.edu.zjicm.wordsnet_d.util.Utils;

public class WordBaseActivity extends BaseNormalActivity {
	public static final int TITLE_BAR_STYLE_LOGO = 0;
	public static final int TITLE_BAR_STYLE_BACK_BUTTON = 1;
	private boolean isInImmersionStatusBarForXiaoMi;

	private ViewGroup mTitleBarLogoContainer;
	private ViewGroup mTitleBarBackButtonContainer;

	// LogoStyle
	private TextView mBookNameTitle;

	// BackStyle
	private TextView mTitleBarBackTitle;
	private TextView mTitleBarBackButton;

	// Right Button
	private ImageView mRightBtn0;// 星星标记
	private TextView mRightBtn1; // 最右Button
	private LinearLayout mRightBtn2;// 次右Button
	// Right Text
	private TextView mRightText, mRightText2;

	private ViewFlipper viewFlipper;

	public EditText searchEditText;
	public TextView searchClearButton;
	public boolean searchTextEmpty;
	private RelativeLayout layout1;
	public static RelativeLayout layout2;
	private boolean isFirstNotEmpty = true;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setTitleView(R.layout.view_title_bar);

		findViews();
		initViews();
		immersionStatusBarForXiaoMi();
	}

	@Override
	protected void onResume() {
		super.onResume();

	}

	/**
	 * 显示titleBar的阴影
	 */
	protected void showTitleBarShadow() {
		// int w = View.MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
		// int h = View.MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
		// layout1.measure(w, h);
		// int height = layout1.getMeasuredHeight();
		// RelativeLayout.LayoutParams rl = new
		// RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
		// height);
		// layout2.setLayoutParams(rl);
		layout2.setVisibility(View.VISIBLE);
	}

	public static void removeShadow() {
		layout2.setVisibility(View.GONE);
	}

	protected void showGuideForAffix() {
		showTitleBarShadow();
		layout2.setBackgroundResource(R.drawable.guide_in_studing_title_bar);
	}

	protected void removeGuideForAffix() {
		removeShadow();
	}

	private void findViews() {
		mTitleBarLogoContainer = (ViewGroup) findViewById(R.id.title_bar_logo_style);
		mTitleBarBackButtonContainer = (ViewGroup) findViewById(R.id.title_bar_back_button_style);

		mBookNameTitle = (TextView) findViewById(R.id.title_bar_bookname_title);

		mTitleBarBackTitle = (TextView) findViewById(R.id.title_bar_back_title);
		mTitleBarBackButton = (TextView) findViewById(R.id.title_bar_back_button);

		mRightBtn0 = (ImageView) findViewById(R.id.title_bar_right_btn0);
		mRightBtn1 = (TextView) findViewById(R.id.title_bar_right_btn1);
		mRightBtn2 = (LinearLayout) findViewById(R.id.title_bar_right_btn2);
		mRightText = (TextView) findViewById(R.id.title_bar_right_text);
		mRightText2 = (TextView) findViewById(R.id.title_bar_right_text2);

		viewFlipper = (ViewFlipper) findViewById(R.id.title_bar_viewflipper);

		searchEditText = (EditText) findViewById(R.id.title_bar_search_edit);
		searchClearButton = (TextView) findViewById(R.id.title_bar_search_clear_button);// 清空搜索内容按钮
		layout1 = (RelativeLayout) findViewById(R.id.wordbase_title_bar_layout);
		layout2 = (RelativeLayout) findViewById(R.id.title_bar_shadow);
	}

	private void initViews() {
		mTitleBarBackButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	public void setBackButton(View.OnClickListener onClick) {
		mTitleBarBackButton.setOnClickListener(onClick);
	}

	public void hideTitleBarBackButton() {
		// mRightBtn2.setVisibility(View.INVISIBLE);
		// setBookNameTitleText("登录");
		mTitleBarBackButton.setVisibility(View.GONE);
	}

	public void hideTitleBarBackButton2() {
		// mRightBtn2.setVisibility(View.INVISIBLE);
		// setBookNameTitleText("设置学习计划");
		mTitleBarBackButton.setVisibility(View.GONE);
	}

	/**
	 * 修改标题栏背景
	 */
	public void changeBackground() {
		LinearLayout titleLayout = (LinearLayout) findViewById(R.id.linearLayout3);
		mTitleBarBackButton.setHeight(10);
	}

	/**
	 * 设置标题Bar样式
	 * 
	 * @param style
	 */
	public void setTitleBarStyle(int style) {
		if (style < 0 || style > 1)
			return;
		switch (style) {
		case TITLE_BAR_STYLE_BACK_BUTTON:
			mTitleBarBackButtonContainer.setVisibility(View.VISIBLE);
			mTitleBarLogoContainer.setVisibility(View.GONE);
			break;
		case TITLE_BAR_STYLE_LOGO:
		default:
			mTitleBarBackButtonContainer.setVisibility(View.GONE);
			mTitleBarLogoContainer.setVisibility(View.VISIBLE);
			break;
		}
	}

	public void setBookNameTitleText(String title) {
		mBookNameTitle.setText(title);
	}

	public void setBookNameTitleGravityCenter() {
		mBookNameTitle.setGravity(Gravity.CENTER);
	}

	/**
	 * 设置标题文字，只在{@link STYLE_LOGO}下有用
	 * 
	 * @param title
	 */
	public void setTitleText(String title) {
		mTitleBarBackTitle.setText(title);
	}

	public void setTitleTextGravityCenter() {
		mTitleBarBackTitle.setGravity(Gravity.CENTER);
	}

	/**
	 * 设置难词标记
	 * 
	 * @param isMarked
	 * @return void
	 * @since 2015.05.27
	 */
	public void setRightButton0(boolean isMarked, View.OnClickListener onClick) {
		mRightBtn0.setVisibility(View.VISIBLE);
		mRightBtn0.setOnClickListener(onClick);
		if (isMarked) {
			mRightBtn0.setImageResource(R.drawable.difficulty_button_yes);
		} else {
			mRightBtn0.setImageResource(R.drawable.difficulty_button_no);
		}
		if (!isInImmersionStatusBarForXiaoMi) {
			// lineLeftToRightText.setVisibility(View.VISIBLE);
			mRightBtn0.setPadding((int) (20 * getResources().getDisplayMetrics().density), 0, (int) (20 * getResources().getDisplayMetrics().density), 0);
		}
	}

	public void setRightButton0(boolean isMarked) {
		mRightBtn0.setVisibility(View.VISIBLE);
		if (isMarked) {
			mRightBtn0.setImageResource(R.drawable.difficulty_button_yes);
		} else {
			mRightBtn0.setImageResource(R.drawable.difficulty_button_no);
		}
		if (!isInImmersionStatusBarForXiaoMi) {
			// lineLeftToRightText.setVisibility(View.VISIBLE);
			mRightBtn0.setPadding((int) (20 * getResources().getDisplayMetrics().density), 0, (int) (20 * getResources().getDisplayMetrics().density), 0);
		}
	}

	public void setRightButton1(String text, int imgResource, View.OnClickListener onClick) {
		mRightBtn1.setVisibility(View.VISIBLE);
		mRightBtn1.setText(text);
		mRightBtn1.setOnClickListener(onClick);
		mRightBtn1.setBackgroundResource(imgResource);
		if (!isInImmersionStatusBarForXiaoMi) {
			mRightBtn1.setPadding((int) (20 * getResources().getDisplayMetrics().density), 0, (int) (20 * getResources().getDisplayMetrics().density), 0);
		}
	}

	public ImageView getRightButton0() {
		return mRightBtn0;
	}

	public TextView getRightButton1() {
		return mRightBtn1;
	}

	public void setRightButton2(String text, View.OnClickListener onClick) {
		mRightBtn2.setVisibility(View.VISIBLE);
		mRightBtn2.setOnClickListener(onClick);
	}

	public void hideRightButton0() {
		mRightBtn0.setVisibility(View.GONE);
		// lineLeftToRightText.setVisibility(View.GONE);
	}

	public void hideRightButton1() {
		mRightBtn1.setVisibility(View.GONE);
	}

	public void hideRightButton2() {
		mRightBtn2.setVisibility(View.GONE);
	}

	public void showRightButton0() {
		mRightBtn0.setVisibility(View.VISIBLE);
	}

	public void showRightButton1() {
		mRightBtn1.setVisibility(View.VISIBLE);
	}

	public void showRightButton2() {
		mRightBtn2.setVisibility(View.VISIBLE);
	}

	public void setRightText(String text, OnClickListener onClickListener) {
		mRightText.setText(text);
		mRightText.setVisibility(View.VISIBLE);
		mRightText.setOnClickListener(onClickListener);
	}

	public void setRightText(String text) {
		mRightText.setText(text);
		mRightText.setVisibility(View.VISIBLE);
	}

	public void setRightText2(String text) {
		mRightText2.setText(text);
		mRightText2.setVisibility(View.VISIBLE);
	}

	public void setRightText2(String text, int imgResource, View.OnClickListener onClick) {
		mRightText2.setVisibility(View.VISIBLE);
		mRightText2.setText(text);
		mRightText2.setOnClickListener(onClick);
		mRightText2.setBackgroundResource(imgResource);
	}

	public TextView getRightText2() {
		return mRightText2;
	}

	public void hideRightText2() {
		mRightText2.setVisibility(View.GONE);
	}

	public void hideRightText() {
		mRightText.setVisibility(View.GONE);
	}

	public void showRightText() {
		mRightText.setVisibility(View.VISIBLE);
	}

	public void showRightText2() {
		mRightText2.setVisibility(View.VISIBLE);
	}

	TextWatcher mTextWatcher;

	public void showSearchView(TextWatcher mTextWatcher, OnClickListener onClickListener, OnEditorActionListener onKey) {
		this.mTextWatcher = mTextWatcher;

		viewFlipper.setDisplayedChild(1);

		searchEditText.addTextChangedListener(mTextWatcher);
		searchEditText.setOnClickListener(onClickListener);
		searchEditText.setOnEditorActionListener(onKey);
		setSearchTextEmpty(true);

		searchClearButton.setOnClickListener(onClickListener);
	}

	public boolean isSearchTextEmpty() {
		return searchTextEmpty;
	}

	public void setSearchTextEmpty(boolean searchTextEmpty) {
		if (this.searchTextEmpty == searchTextEmpty)
			return;

		this.searchTextEmpty = searchTextEmpty;
		if (searchTextEmpty) {
			// searchEditText.removeTextChangedListener(mTextWatcher);
			// searchEditText.setText("请输入要搜索的单词");
			// searchEditText.setSelection(0);// 设置光标位置
			// searchEditText.setTextColor(Color.GRAY);
			// searchEditText.addTextChangedListener(mTextWatcher);
			searchEditText.setText("");

			searchClearButton.setVisibility(View.GONE);
			isFirstNotEmpty = true;
		} else if (isFirstNotEmpty) {
			if (NightModeUtil.isNightModeNow())
				searchEditText.setTextColor(getResources().getColor(R.color.word_color_night));
			else
				searchEditText.setTextColor(Color.BLACK);
			searchClearButton.setVisibility(View.VISIBLE);
			isFirstNotEmpty = false;
		}
	}

	private void immersionStatusBarForXiaoMi() {
		isInImmersionStatusBarForXiaoMi = Utils.immersionStatusBarForXiaoMi(WordBaseActivity.this);
		if (isInImmersionStatusBarForXiaoMi) {
			layout1.setPadding(0, Utils.changeDipToPx(WordBaseActivity.this, 20), 0, 0);
			immersionBaseStatusBarForXiaoMi();
		}
	}
}
